package com.eastjet.maintenance.contrller;

import com.eastjet.maintenance.activti.ActivtiServiceimp;
import com.eastjet.maintenance.config.Code;
import com.eastjet.maintenance.config.Result;
import com.eastjet.maintenance.domain.*;
import com.eastjet.maintenance.feign.CodeDataFeing;
import com.eastjet.maintenance.service.impl.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.security.utils.SecurityUtils;
//import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/application")
@CrossOrigin(origins = "*")
public class ApplicationController {

    @Autowired
    private ApplicationServiceImpl applicationService;// 申请采购
    @Autowired
    private PurchasedetailsServiceImpl purchasedetailsService;// 采购明细
    @Autowired
    private CodeDataFeing codeDataFeing;//自动生成代码
    @Autowired
    private DocumentationServiceImpl documentationService;// 文档管理
    @Autowired
    private PurchaseorderServiceImpl purchaseorderService;// 采购订单
    @Autowired
    private AcceptanceServiceImpl acceptanceService;// 验收单

    /*
     * 新增申请采购/采购明细
     */
    @PostMapping("/insert")
    public Result InsertApplication(@RequestBody Map<String, Object> map) {
        Boolean b = applicationService.InsertApplication(map);
        return new Result(b, b ? Code.SAVE_OK : Code.SAVE_ERR, b ? "保存成功" : "保存失败");
    }

    /*
     * 新增备件申请采购/采购明细
     */
    @PostMapping("/insertbei")
    public Result InsertApplicationBei(@RequestBody Map<String, Object> map) {
        System.out.println("新增备件:" + map.keySet());
        Boolean b = applicationService.InsertApplicationBei(map);
        return new Result(b, b ? Code.SAVE_OK : Code.SAVE_ERR, b ? "保存成功" : "保存失败");
    }


    /*
    修改采购申请
     */
    @PostMapping("/update")
    public Result UpdateApplication(Application application) {
        Boolean b = applicationService.UpdateApplication(application);
        return new Result(b, b ? Code.UPDATE_OK : Code.UPDATE_ERR, b ? "更新成功" : "更新失败");
    }

    /*
     * 删除采购申请
     */
    @PostMapping("/delete")
    public Result DeleteApplication(Application application) {
        Boolean b = applicationService.DeleteApplication(application);
        return new Result(b, b ? Code.DELETE_OK : Code.DELETE_ERR, b ? "删除成功" : "删除失败");
    }

    /*
    查看所有申请采购
     */
    @RequestMapping("/select")
    public Result SelectApplication(Application application, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        System.out.println("查看所有申请采购" + application.getPan());
        List<Application> applications = applicationService.selectAllApplication(application);
        PageInfo<Application> pageInfo = new PageInfo<>(applications);
        return new Result(pageInfo, applications.isEmpty() ? Code.GET_ERR : Code.GET_OK, applications.isEmpty() ? "查询失败" : "查询成功");
    }

    /*
    查看所有申请采购详细
     */
    @PostMapping("/selectAll")
    public Result SelectApplicationAll(@RequestBody Map<String, Object> map) {
        Map<String, Object> map2 = new HashMap<>();
        List<Application> list = applicationService.selectAllApplication(new Application() {{
            setCode(map.get("code").toString());
            setPan(Integer.valueOf(map.get("pan").toString()));
        }});
        List<Application> list2 = applicationService.selectAllApplication(new Application() {{
            setCreatedBy(map.get("username").toString());
            setPan(Integer.valueOf(map.get("pan").toString()));
        }});
        List<Purchasedetails> purchasedetails = purchasedetailsService.selectPurchasedetailsById(map.get("code").toString());
        List<Documentation> docun = documentationService.SelectDocumentation(map.get("code").toString());
        map.put("list", list);
        map.put("list2", list2);
        map.put("purchasedetails", purchasedetails);
        map.put("docun", docun);
        return new Result(map, map.isEmpty() ? Code.GET_ERR : Code.GET_OK, map.isEmpty() ? "查询失败" : "查询成功");
    }

    /*
    新增采购明细
     */
//    @PostMapping("/InsertPurchasedetails")
//    public Result InsertPurchasedetails(@RequestBody List<Purchasedetails> purchasedetails) {
//        for (Purchasedetails p : purchasedetails) {
//            p.setRequestNumber(codeDataFeing.test(new HashMap<String, String>() {{
//                put("TableName", "application");
//            }}));
//        }
//        System.out.printf("新增采购明细:" + purchasedetails);
//        Boolean b = purchasedetailsService.InsertPurchasedetails(purchasedetails);
//        return new Result(b, b ? Code.SAVE_OK : Code.SAVE_ERR, b ? "新增成功" : "新增失败");
//    }

    /*
    修改采购明细
     */
    @PostMapping("/UpdatePurchasedetails")
    public Result UpdatePurchasedetails(List<Purchasedetails> purchasedetails) {
        Boolean b = purchasedetailsService.UpdatePurchasedetails(purchasedetails);
        return new Result(b, b ? Code.UPDATE_OK : Code.UPDATE_ERR, b ? "更新成功" : "更新失败");
    }

    @PostMapping("/UpdatePurchasedetails2")
    public String UpdatePurchasedetails(@RequestBody Map<String, Object> map) {
        System.out.println("修改采购明细:" + map.keySet());
        System.out.println("修改采购明细2:" + map.get("tableData"));
        System.out.println("修改采购明细2:" + map.get("documents"));
        return "success";
    }

    /**
     * 查询申请采购状态
     *
     * @param str
     * @return
     */
    @RequestMapping("/SelctaQi")
    public Result SelectApplicationAllQi(@RequestParam(value = "str") String str) {
        List<Application> list = applicationService.selectAllApplicationQi(str);
        return new Result(list, list.isEmpty() ? Code.GET_ERR : Code.GET_OK, list.isEmpty() ? "查询失败" : "查询成功");
    }

    /**
     * 新增采购订单
     *
     * @param map
     * @return
     */
    @PostMapping("/InsertOrder")
    public Result InsertOrder(@RequestBody Map<String, Object> map) {
        Boolean b = purchaseorderService.InsertOrder(map);
        System.out.println("新增采购订单:" + b);
        return new Result(b, b ? Code.SAVE_OK : Code.SAVE_ERR, b ? "新增成功" : "新增失败");
    }

    /**
     * 查询采购订单列表
     *
     * @param purchaseorder
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/SelectListOrder")
    public Result SelectListOrder(Purchaseorder purchaseorder, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        System.out.println("查询采购订单列表:" + purchaseorder.getState() + "数据:" + purchaseorder.getCode());
        PageHelper.startPage(pageNum, pageSize);
        List<Purchaseorder> list = purchaseorderService.SelectOrderList(purchaseorder);
        PageInfo<Purchaseorder> pageInfo = new PageInfo<>(list);
        return new Result(pageInfo, list.isEmpty() ? Code.GET_ERR : Code.GET_OK, list.isEmpty() ? "查询失败" : "查询成功");
    }

    /**
     * 查询申请采购订单对应明细
     *
     * @param purchaseorder
     * @return
     */
    @RequestMapping("/SelectOrderXi")
    public Result SelectOrderXi(@RequestParam("code") String code) {
        List<Purchasedetails> purchasedetails = purchasedetailsService.selectPurchasedetailsById(code);
        return new Result(purchasedetails, purchasedetails.isEmpty() ? Code.GET_ERR : Code.GET_OK, purchasedetails.isEmpty() ? "查询失败" : "查询成功");
    }

    /**
     * 根据采购订单号查询对应的采购明细
     *
     * @param purchaseorder
     * @return
     */
    @RequestMapping("/SelectOrderList")
    public Result SelectOrderList(Purchasedetails purchasedetails, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        System.out.println("根据订单好查询采购明细2f" + purchasedetails.getPurchaseOrder());
        PageHelper.startPage(pageNum, pageSize);
        List<Purchasedetails> list = purchasedetailsService.selectPurchasedetailsByCode(purchasedetails);
        PageInfo<Purchasedetails> pageInfo = new PageInfo<>(list);
        return new Result(pageInfo, list.isEmpty() ? Code.GET_ERR : Code.GET_OK, list.isEmpty() ? "查询失败" : "查询成功");
    }

    /**
     * 查看订单信息
     *
     * @return
     */
    @RequestMapping("/SelectOrderAll")
    public Result SelectOrderAll(Purchaseorder purchaseorder) {
        System.out.println("查看订单信息:" + purchaseorder.getCode() + ';' + purchaseorder.getPan());
        Map<String, Object> map = new HashMap<>();
        List<Purchaseorder> list = purchaseorderService.SelectOrderList(purchaseorder);
        List<Documentation> documentations = documentationService.SelectDocumentation(purchaseorder.getCode());
        List<Purchasedetails> list2 = purchasedetailsService.selectPurchasedetailsByCode(new Purchasedetails() {{
            setPurchaseOrder(purchaseorder.getCode());
        }});
        List<Acceptance> list3 = acceptanceService.SelectByAcceptanceId(new Acceptance() {{
            setPurchase(purchaseorder.getCode());
        }});
        map.put("list", list);
        map.put("list2", list2);
        map.put("documentations", documentations);
        map.put("list3", list3);
        return new Result(map, list.isEmpty() ? Code.GET_ERR : Code.GET_OK, list.isEmpty() ? "查询失败" : "查询成功");
    }

    @RequestMapping("/SelectAcceptanceList")
    public Result SelectAcceptanceList(Acceptance acceptance) {
        System.out.println("查询验收单列表:" + acceptance.getCode() + "数据:" + acceptance.getPurchase());
        Map<String, Object> map = new HashMap<>();
        List<Acceptance> list = acceptanceService.SelectByAcceptanceId(acceptance);
        List<Purchasedetails> list1 = purchasedetailsService.selectPurchasedetailsByCode(new Purchasedetails() {{
            setPurchaseOrder(acceptance.getPurchase());
        }});
        List<Documentation> documentations = documentationService.SelectDocumentation(acceptance.getCode());
        map.put("list", list);
        map.put("list1", list1);
        map.put("documentations", documentations);
        return new Result(map, list.isEmpty() ? Code.GET_ERR : Code.GET_OK, list.isEmpty() ? "查询失败" : "查询成功");
    }


}

